package com.xiaotu.common.utiles;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

public class FileUtil {
    //文件下载
    public static void download(String filename, HttpServletResponse httpServletResponse) throws Exception{


        //发送给客户端的数据
        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
        byte[] bytes = new byte[1024];
        BufferedInputStream bufferedInputStream = null;
        //读取filename
        bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("./file/" + filename)));
        int i = bufferedInputStream.read(bytes);
        while (i != 1){
            outputStream.write(bytes,0,bytes.length);
            outputStream.flush();
            i = bufferedInputStream.read(bytes);
        }
        bufferedInputStream.close();
        outputStream.close();
    }
}
